<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
class Notice extends Base
{
    /**
     * 公告列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function NoticeList(){
        //总记录条数
        $Count = NoticeCount();
        $this->assign('Count',$Count);

        //公告列表
        $NoticeList = NoticeList();
        $this->assign('NoticeList',$NoticeList['NoticeList']);

        //分页
        $this->assign('Page', $NoticeList['Page']);

        return $this->fetch('NoticeList');
    }


    /**
     * 公告添加
     * @return array|mixed
     */
    public function NoticeAdd(){
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            $Data['add_time'] = time();
            //向表中添加数据
            if(Db::name('notice')->insert($Data)){
                return ['status'=>1,'message'=>'添加成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        return $this->fetch('NoticeAdd');
    }

    /**
     * 公告编辑
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function NoticeEdit(){
        $NoticeInfo = NoticeInfo(['id'=>Param('id')]);
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            $Data['add_time'] = time();
            //向表中添加数据
            if(Db::name('notice')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        $this->assign('NoticeInfo',$NoticeInfo);
        return $this->fetch('NoticeEdit');
    }

    /**
     * 公告删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function NoticeDelete(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            if(Db::name('notice')->where(['id'=>Param('id')])->delete()){
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }
}